<script setup lang="ts">
import { getCategoryAPI, getSubCategoryAPI } from '@/services/category'
import { getGoodsByClassNumber } from '@/services/goods'
import type { Class, subClass } from '@/types/category'
import { onLoad, onShow } from '@dcloudio/uni-app'
import { ref } from 'vue'
import PageSeletop from './components/PageSeletop.vue'
import { useCateGoryStore } from '@/stores/modules/category'
import type { ProducTItem } from '@/types/global'
import { watch } from 'vue'
// // 获取轮播图的数据
// const getBannerData = async () => {
//   const res = await getHomeBannerAPI()
// }
// 获取分类id的仓库
const CateGoryStore = useCateGoryStore()
// 获取分类列表数据
const categoryList = ref<Class>()
// 左侧导航栏的序号
const activeIndex = ref(0)
const getCategoryTopData = async () => {
  const res = (await getCategoryAPI()).data.filter(item => { return item.rootClassId == 'null' })
  if (res.length > 0) {
    categoryList.value = res
    getSubCategory()
  }
}
//二级分类的数据
const subCategoryList = ref<subClass>()
//一大类下的商品
let goodsList = ref<[ProducTItem][]>([])
const getSubCategory = async () => {
  if (categoryList.value) {
    let a = categoryList.value[activeIndex.value].classNumber
    subCategoryList.value = (await getSubCategoryAPI(a)).data
    await getSubCategoryList()
  }
}
// 获取当前二级分类的数据
const getSubCategoryList = (async () => {
  for (let i in subCategoryList.value) {
    let items = await getGoodsByClassNumber(subCategoryList.value[Number(i)].classNumber)
    goodsList.value.push(items)
  }
})
watch(() => activeIndex.value, () => {
  goodsList.value = []
  getSubCategory()
})

// 数据是否加载完成
const isFinish = ref(false)
onLoad(async () => {
  isFinish.value = false
  if (CateGoryStore.cateGory) {
    activeIndex.value = CateGoryStore.cateGory
  }
  await Promise.all([getCategoryTopData(), getSubCategoryList()])
  isFinish.value = true
})
onShow(async () => {
  if (CateGoryStore.cateGory) {
    activeIndex.value = CateGoryStore.cateGory
  }
})
console.log(goodsList)
</script>

<template>
  <PageSeletop v-if="!isFinish" />
  <view class="viewport" v-if="isFinish">
    <!-- 搜索框 -->
    <navigator url="/pages/search/index" open-type="navigate" hover-class="navigator-hover" icon="search">
      <!-- 搜索条 -->
      <view class="search">
        <text class="icon-search">搜索商品</text>
      </view>
    </navigator>
    <!-- 分类 -->
    <view class="categories">
      <!-- 左侧：一级分类 -->
      <scroll-view class="primary" scroll-y>
        <view v-for="(item, index) in categoryList" :key="item._id" class="item"
          :class="{ active: index === activeIndex }" @tap="activeIndex = index">
          <text class="name"> {{ item.className }} </text>
        </view>
      </scroll-view>
      <!-- 右侧：二级分类 -->
      <scroll-view class="secondary" scroll-y>
        <!-- 焦点图 -->
        <XTxSwiper class="banner" :list="[]" />
        <!-- 内容区域 -->
        <view class="panel" v-if="subCategoryList" v-for="(item, index) in subCategoryList" :key="item._id">
          <view class="title">
            <text class="name">{{ item.className }}</text>
            <navigator class="more" hover-class="none">全部</navigator>
          </view>
          <view class="section">
            <navigator v-for="goods in goodsList[index]" :key="goods._id" class="goods" hover-class="none"
              :url="`/pages/goods/index?id=${goods._id}`">
              <image class="image" :src="goods.images[goods.cover]"></image>
              <view class="name ellipsis">{{ goods.name }}</view>
              <view class="price">
                <text class="symbol">¥</text>
                <text class="number">{{ goods.price.sale }}</text>
              </view>
            </navigator>
          </view>
        </view>
      </scroll-view>
    </view>
  </view>
</template>

<style lang="scss">
page {
  height: 100%;
  overflow: hidden;
}

.viewport {
  height: 100%;
  display: flex;
  flex-direction: column;

  .search {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0 10rpx 0 26rpx;
    height: 64rpx;
    margin: 16rpx 20rpx;
    color: #777;
    font-size: 28rpx;
    border-radius: 32rpx;
    background-color: rgba(222, 222, 222, 0.6);
  }

  .icon-search {
    &::before {
      margin-right: 10rpx;
    }
  }
}

/* 分类 */
.categories {
  flex: 1;
  min-height: 400rpx;
  display: flex;
}

/* 一级分类 */
.primary {
  overflow: hidden;
  width: 180rpx;
  flex: none;
  background-color: #f6f6f6;

  .item {
    display: flex;
    justify-content: center;
    align-items: center;
    height: 96rpx;
    font-size: 26rpx;
    color: #595c63;
    position: relative;

    &::after {
      content: '';
      position: absolute;
      left: 42rpx;
      bottom: 0;
      width: 96rpx;
      border-top: 1rpx solid #e3e4e7;
    }
  }

  .active {
    background-color: #fff;

    &::before {
      content: '';
      position: absolute;
      left: 0;
      top: 0;
      width: 8rpx;
      height: 100%;
      background-color: #27ba9b;
    }
  }
}

.primary .item:last-child::after,
.primary .active::after {
  display: none;
}

/* 二级分类 */
.secondary {
  background-color: #fff;

  .carousel {
    height: 200rpx;
    margin: 0 30rpx 20rpx;
    border-radius: 4rpx;
    overflow: hidden;
  }

  .panel {
    margin: 0 30rpx 0rpx;
  }

  .title {
    height: 60rpx;
    line-height: 60rpx;
    color: #333;
    font-size: 28rpx;
    border-bottom: 1rpx solid #f7f7f8;

    .more {
      float: right;
      padding-left: 20rpx;
      font-size: 24rpx;
      color: #999;
    }
  }

  .more {
    &::after {
      font-family: 'erabbit' !important;
      content: '\e6c2';
    }
  }

  .section {
    width: 100%;
    display: flex;
    flex-wrap: wrap;
    padding: 20rpx 0;

    .goods {
      width: 220rpx;
      margin: 0rpx 30rpx 20rpx 0;

      &:nth-child(3n) {
        margin-right: 0;
      }

      image {
        width: 180rpx;
        height: 180rpx;
        margin: 0 20rpx;
      }

      .name {
        padding: 5rpx;
        font-size: 22rpx;
        color: #333;
      }

      .price {
        padding: 5rpx;
        font-size: 18rpx;
        text-indent: 18rpx;
        color: #cf4444;
      }

      .number {
        font-size: 24rpx;
        margin-left: 2rpx;
      }
    }
  }
}
</style>
